
package com.dev.su.filter;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.dev.su.util.TextUtils;

public class URLMatcher {
    // 编码
    private static final String ECODING = "UTF-8";
    // 获取img标签正则
    private static final String IMGURL_REG = "<img.*src=(\".*?\").*>";
    // 获取src路径的正则
    private static final String IMGSRC_REG = "(https|http)://[^\\s]*(jpg|png|jpeg|bmp|gif){1}";

    public static String getHtml(String urlString) {
        try {
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();
            InputStream inputStream = connection.getInputStream();
            byte[] buffer = new byte[1024];
            StringBuilder builder = new StringBuilder();
            int count = 0;
            while ((count = inputStream.read(buffer)) > 0) {
                builder.append(new String(buffer, 0, count));
            }
            inputStream.close();
            return builder.toString();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /***
     * 获取ImageUrl地址
     * 
     * @param HTML
     * @return
     */
    public static List<String> getImageUrl(String HTML) {
        Matcher matcher = Pattern.compile(IMGURL_REG).matcher(HTML);
        List<String> listImgUrl = new ArrayList<String>();
        while (matcher.find()) {
            String group = matcher.group(1);
            System.out.println(group);
            listImgUrl.add(group);
        }
        return listImgUrl;
    }

    final static Pattern SRC_PATTERN;
    static {
        SRC_PATTERN = Pattern.compile(IMGSRC_REG, Pattern.CASE_INSENSITIVE);
    }
    private static String LINK = "<.*?href=\"([^\\s]*?)\".*?>";

    private static String HTTPPATTERN = "((https|http|ftp|rtsp|mms)?:\\\\)[^\\s]+";
    final static Pattern HTTP_PATTERN = Pattern.compile(HTTPPATTERN);
    final static Pattern LINK_PATTERN = Pattern.compile(LINK);

    public static List<String> getLinks(String html) {
        Matcher matcher = LINK_PATTERN.matcher(html);
        List<String> links = new ArrayList<String>();
        while (matcher.find()) {
            String group = matcher.group(1);
            Matcher matcher2 = HTTP_PATTERN.matcher(group);
            links.add(group);
            if (TextUtils.isEmpty(group)) {
                System.out.println(matcher.group(0));
            }
        }
        return links;

    }

    public static String getRealImgSrc(String urlWithHtml) {
        if (!TextUtils.isEmpty(urlWithHtml)) {
            Matcher matcher = SRC_PATTERN.matcher(urlWithHtml);
            if (matcher.find()) {
                return matcher.group(0);
            }

        }
        return "";

    }

}
